package com.foot.travel.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.foot.travel.R;
import com.koalac.ironman.common.module.ModuleActivity;
import com.koalac.ironman.net.MyRequest;
import com.koalac.ironman.net.ResponseCallback;
import com.koalac.ironman.net.pojo.request.CRegister;
import com.koalac.ironman.net.pojo.request.RegisterWeiXin;
import com.koalac.ironman.net.pojo.response.Response;
import com.koalac.ironman.util.ScreenInfoUtil;
import com.koalac.ironman.util.StringUtils;
import com.koalac.ironman.viewhelper.ViewInject;
import com.koalac.ironman.widget.LoadingDialog;

public class SetPasswordActivity extends ModuleActivity implements
	OnClickListener {

    @ViewInject(id = R.id.parent_view)
    View mParentView;
    @ViewInject(id = R.id.password)
    EditText mPasswordView;
    @ViewInject(id = R.id.re_password)
    EditText mRePasswordView;
    @ViewInject(id = R.id.register_btn, click = "onClick")
    TextView mRegisterBtn;

    private final int PASSWORD_LENGTH = 6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setNavigationTitle("注册");
	setBackButtonVisibility(true);
	setContentLayoutBackgroudColor(getResources().getColor(
		R.color.module_color1));
	int bottomHeight = ScreenInfoUtil.getScreenHeight(SetPasswordActivity.this) - ScreenInfoUtil.dp2px(SetPasswordActivity.this, 290);
	checkKeyboardHeight(mParentView, bottomHeight);
    }

    @Override
    protected void initLayout(ViewGroup rootContentView) {
	addContentView(R.layout.activity_set_password);
    }

    private boolean checkInput() {
	if (StringUtils.isEmpty(mPasswordView.getText().toString())) {
	    toast("密码为空！");
	    return false;
	}
	if (mPasswordView.getText().toString().length() < PASSWORD_LENGTH) {
	    toast("密码的位数至少为6位！");
	    return false;
	}

	if (StringUtils.isEmpty(mRePasswordView.getText().toString())) {
	    toast("请再次确认密码!");
	    return false;
	}
	if (!mRePasswordView.getText().toString()
		.equals(mPasswordView.getText().toString())) {
	    toast("两次密码不一致!");
	    return false;
	}
	return true;
    }

    @Override
    public void onClick(View v) {
	int viewId = v.getId();
	switch (viewId) {
	case R.id.register_btn:
	    if (checkInput()) {
		
		if(!StringUtils.isEmpty(getIntent().getStringExtra(RegisterActivity.OPEN_ID))) {
		    weixinRegister();
		}else {
		    commonRegister();
		}
	    }
	    break;
	}
    }
    
    private void weixinRegister(){
	RegisterWeiXin registerWeiXin = new RegisterWeiXin();
	registerWeiXin.setUser_name(getIntent().getStringExtra(
		RegisterActivity.MOBILE_KEY));
	registerWeiXin.setDevice_id("0");
	registerWeiXin.setOpenid(getIntent().getStringExtra(RegisterActivity.OPEN_ID));
	registerWeiXin.setNickname(getIntent().getStringExtra(RegisterActivity.NICK_NAME));
	registerWeiXin.setHeadimgurl(getIntent().getStringExtra(RegisterActivity.HEADIMGURL));
	registerWeiXin.setPassword(mRePasswordView.getText().toString());
	MyRequest.request(SetPasswordActivity.this, registerWeiXin, new ResponseCallback() {
	    
	    @Override
	    public void process(boolean result, Context context, Response data,
		    String msg) {
			if (result) {
			    returnLoginActivity();
			}
	    }
	});
    }
    
    private void commonRegister() {
	CRegister cRegister = new CRegister();
	cRegister.setUserName(getIntent().getStringExtra(
		RegisterActivity.MOBILE_KEY));
	cRegister.setPassword(mRePasswordView.getText().toString());
	cRegister
		.setDeviceId(getRegistrationID(SetPasswordActivity.this));
	final LoadingDialog p = showLoadingDialog("正在注册...");
	MyRequest.request(SetPasswordActivity.this, cRegister,
		new ResponseCallback() {

		    @Override
		    public void process(boolean result,
			    Context context, Response data, String msg) {
			canceledLoadingDialog(p);
			returnLoginActivity();
		    }
		});
    }
    
    private void returnLoginActivity() {
	finishedAll();
	/**
	 * 调到主界面
	 */
	Intent intent = new Intent(
		SetPasswordActivity.this,
		LoginActivity.class);
	startActivity(intent);
    }
}
